package com.qiankan.qmsj.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.qiankan.qmsj.R;
import com.qiankan.qmsj.UserInfo;
import com.qiankan.qmsj.adapter.CommonAdapter;
import com.qiankan.qmsj.db.UserDBHelper;
import com.qiankan.qmsj.entity.Discuss;
import com.qiankan.qmsj.entity.Product;
import com.qiankan.qmsj.entity.Result;
import com.qiankan.qmsj.net.APIHub;
import com.qiankan.qmsj.utils.AsyncLoadImage;
import com.qiankan.qmsj.viewHolder.CommentViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CommentActivity extends BaseActivity {
    @BindView(R.id.rv_Commend)
    RecyclerView mRvCommend;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    private List<Discuss> commentEntityList = new ArrayList<>();
    private CommonAdapter<Discuss> commentAdapter;
    private Product product;

    public static Intent newIntent(Context context, Product product) {
        Intent intent = new Intent(context, CommentActivity.class);
        intent.putExtra("product", product);
        return intent;
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_comment;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        product = (Product) getIntent().getSerializableExtra("product");
        if (product == null) {
            return;
        }
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refresh();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        refresh();
    }

    private void initCommend() {
        mRvCommend.setHasFixedSize(true);
        mRvCommend.setLayoutManager(new LinearLayoutManager(this));
//        mRvCommend.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
        commentAdapter = new CommonAdapter<Discuss>(this, CommentViewHolder.class, commentEntityList, R.layout.comment_item) {
            @Override
            public void convert(RecyclerView.ViewHolder holder, final Discuss item, int position) {
                CommentViewHolder viewHolder = (CommentViewHolder) holder;
                AsyncLoadImage.loadImageRound(item.getCustomer().getHead(), viewHolder.mImageHeader, R.mipmap.wd_mrtx);
                viewHolder.mCommentTime.setText(item.getCreatetime());
                viewHolder.mCommentContent.setText(item.getContent());
                viewHolder.mCommentUser.setText("用户" + item.getCustomer().getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                viewHolder.mCommentGrade.setRating(TextUtils.isEmpty(item.getSatisfaction()) ? 5 : Float.parseFloat(item.getSatisfaction()));
            }
        };
        mRvCommend.setAdapter(commentAdapter);
    }

    private void refresh() {
        showLoadingDialog();
        Map<String, String> param = new HashMap<String, String>();
        param.put("productId", product.getId());
        APIHub.getsInstance().getBaseService().getDiscussDetail(param).enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                smartRefreshLayout.finishRefresh();
                dismissLoadingDialog();
                Result result = response.body();
                if (result.getCode().equals("200")) {
                    commentEntityList = new Gson().fromJson(result.getData(), new TypeToken<List<Discuss>>() {
                    }.getType());
                    initCommend();
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                smartRefreshLayout.finishRefresh();
                dismissLoadingDialog();
            }
        });
    }

    @OnClick(R.id.btn_addCommend)
    public void onClickAddCommend(View view) {
        UserInfo userInfo = UserDBHelper.getInstance(this).getUserInfo();
        if (userInfo == null) {
            Intent intent = LogInActivity.newIntent(this);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();
        } else {
            Intent intent = AddCommentActivity.newIntent(this, product);
            startActivity(intent);
        }
    }

    @OnClick(R.id.iv_back)
    public void onClickBack(View view) {
        finish();
    }
}
